﻿// by xzsong.seu@gmail.com, 2008-09-20
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Text.RegularExpressions;

namespace Helper
{
    public static class ContentHelper
    {
        public static string GetMidString(string str, string start, string end)
        {
            int pos;
            return GetMidString(str, start, end, out pos);
        }

        public static int GetMidInteger(string str, string start, string end)
        {
            int pos;
            return GetMidInteger(str, start, end, out pos);
        }

        public static long GetMidLong(string str, string start, string end)
        {
            int pos;
            return GetMidLong(str, start, end, out pos);
        }


        public static int GetMidInteger(string str, string start, string end, out int pos)
        {
            string content = GetMidString(str, start, end, out pos);
            return GetInteger(content);
        }

        public static long GetMidLong(string str, string start, string end, out int pos)
        {
            string content = GetMidString(str, start, end, out pos);
            return GetLong(content);
        }

        public static string GetMidString(string str, string start, string end, out int pos)
        {
            pos = -1;
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end))
            {
                return null;
            }

            int index = str.IndexOf(start, StringComparison.CurrentCultureIgnoreCase);
            if (-1 == index)
            {
                return null;
            }
            index += start.Length;
            int num2 = str.IndexOf(end, index, StringComparison.CurrentCultureIgnoreCase);
            if (-1 == num2)
            {
                return null;
            }
            pos = num2;
            return str.Substring(index, num2 - index).Trim();
        }

        public static int GetInteger(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return -1;
            }
            int id;
            if (int.TryParse(str, out id))
            {
                return id;
            }
            return -1;
        }

        public static long GetLong(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return -1;
            }
            long id;
            if (long.TryParse(str, out id))
            {
                return id;
            }
            return -1;
        }

        public static List<int> GetIntegerList(string str, string del)
        {
            List<int> result = new List<int>();
            if (string.IsNullOrEmpty(str))
            {
                return result;
            }
            string[] parts = str.Split(del.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string part in parts)
            {
                int id = GetInteger(part);
                if (id != -1)
                {
                    result.Add(id);
                }
            }
            return result;
        }

        public static List<int> GetIntegerList(string str)
        {
            return GetIntegerList(str, ",");
        }

        public static string NoHtml(string result)
        {
            if (result == null)
            {
                return string.Empty;
            }
            StringBuilder builder = new StringBuilder();
            bool flag = false;
            foreach (char ch in result)
            {
                switch (ch)
                {
                    case '<':
                        flag = true;
                        break;

                    case '>':
                        flag = false;
                        break;

                    default:
                        if (!flag)
                        {
                            if (ch == '\n')
                            {
                                builder.Append(" ");
                            }
                            else if (((ch != ' ') && (ch != '\t')) && (ch != '\r'))
                            {
                                builder.Append(ch);
                            }
                        }
                        break;
                }
            }
            return builder.ToString().Trim().Replace("&yen;", "￥").Replace("&nbsp;", "");
        }

        /// <summary>
        /// Get verify
        /// by zhuangjq@gmail.com, 2008.10.7
        /// </summary>
        /// <returns>Verify code</returns>
        public static string GetVerifyCode(string content)
        {
            string quota = "\"";
            string Pattern = @"var\s*?g_verify\s*?=\s*?" + quota + @"(.*?)\s*?" + quota + ";";
            Match verifyVar = Regex.Match(content, Pattern, RegexOptions.Multiline);
            return verifyVar.Result("$1").ToString().Trim();
        }

        //获取好友的好友id by wzlichen
        private static Collection<int> getFFId(HttpHelper _hh, int FId)
        {
            string strId, content = "", tmpstr = "末页";
            int page = 0;
            int pos;
            Collection<int> FFId = new Collection<int>();
            Regex regex = new Regex(@".*在线.*/home/\?uid=\d+"".*", RegexOptions.Multiline);
            do
            {
                tmpstr = _hh.Get("http://www.kaixin001.com/friend/?uid=" + FId.ToString() + "&start=" + page.ToString());
                content += tmpstr;
                page += 40;
            } while (tmpstr.Contains("末页") && tmpstr.Contains("下一页"));
            MatchCollection match = regex.Matches(content);
            foreach (Match m in match)
            {
                strId = GetMidString(m.Value, "?uid=", "\"", out pos);
                if (!FFId.Contains(Int32.Parse(strId)))
                {
                    FFId.Add(Int32.Parse(strId));
                }
            }
            return FFId;
        }

        //登录帐号 获取好友id by wzlichen
        private static void LogonAndGetFriendsId(HttpHelper _hh, int myId, Collection<int> _myFriendsList)
        {
            int pos = 0;
            int page = 0;
            string content;
            string strId;
            Regex regex = new Regex(@"/home/\?uid=\d+"".*", RegexOptions.Multiline);
            do
            {
                content = _hh.Get("http://www.kaixin001.com/friend/?uid=" + myId.ToString() + "&viewtype=&start=" + page.ToString());
                MatchCollection match = regex.Matches(content);
                foreach (Match m in match)
                {
                    strId = GetMidString(m.Value, "uid=", "\"", out pos);
                    if (!_myFriendsList.Contains(Int32.Parse(strId)))
                    {
                        _myFriendsList.Add(Int32.Parse(strId));
                    }
                }
                page = page + 40;
            } while (content.IndexOf("下一页") != -1 && content.IndexOf("末页") != -1);
        }
    }
}
